Ext.define('wmf.controller.home.home', {
    extend: 'Ext.app.Controller',
    stores: ['home.tree'],
    models: ['home.tree'],
    views: ['home.center', 
            'home.header', 
            'home.treeMain',
            'home.homePage',
            'Viewport'
    ],
    
    refs: [{
		ref: 'mainWest',
		selector: 'mainWest'
    },{
    	ref: 'mainCenter',
		selector: 'mainCenter'
	}],
	
    init: function () {
        this.control({
        	'mainWest': {
        		itemclick: this.menuClick
            },
            'mainCenter':{
            	
            }
        });
        this.loadData();
    },
    
    createMenuPanel: function(menu){
    	var me = this;
    	return {
		      xtype:'panel',
		      title:menu.text,
		      layout:'fit',
		      border: false,
		      //icon: 'resources/icons/' + menu.iconCls + '.png',
			  items:[{
				  hideHeaders:true,
				  split: true,
				  width: 150,
				  border : false,
				  minWidth: 100,
				  maxWidth: 400,
				  lines: false, 
				  autoScroll: true,
				  anminate: true,
				  rootVisible: false,
				  xtype:'treepanel',
				  store: {
					  root:{
						  id: menu.id,
						  text: menu.text,
					      expanded: true,
					      draggable:false
					  },
					  rootVisible: true,
					  proxy : {  
						  type : 'ajax',  
						  url : 'loadTree.do'  
					  }, 
		        	  params:{node:0}
				  },
				  
				  listeners:{"itemclick": function(tree, node, item, index, e, eOpts){
                       if(node.isLeaf()) {
                          me.addTab(node.data);
                       }
                       else {
                    	   if(!node.isExpanded()) node.expand();
                    	   else node.collapse(); 
                    	}
                  }}
			  }]
    	};
    },
    
    
    loadData: function() {
    	var me = this;
    	Ext.Ajax.request({
      	    url: contextPath+'/loadTree.do',
      	    params:{node:0},
      	    success: function(response, options){
              	var westPanel = me.getMainWest();
                if(westPanel){
                  	westPanel.removeAll();
                }
                var menusArray = Ext.JSON.decode(response.responseText); 
                for(var i=0; i<menusArray.length; i=i+1){ 
                	var mp = me.createMenuPanel(menusArray[i]);
                	westPanel.add(mp); 
                }
                westPanel.doLayout();
      	    },
      	    failure: function(respond, action){
      	    	Ext.Msg.alert("提示", action.result.msg ? action.result.msg : "网络错误");
      	    }
    	});
    },
    
    menuClick: function (tree, record, item, index, e, eOpts) {
    	if(record.isLeaf()) {
			var target = record.data.hrefTarget;
			if(!target) {
				//Ext.Msg.alert($('wmf.common.alert'),$('wmf.common.errors.targetempty'));
			    return;
			}
			this.addTab(record.data);
		}else {
			if(!record.isExpanded()) record.expand();
			else record.collapse(); 
		}
    },
    
    addTab: function(node){
        var target = node.hrefTarget;
        if(!target) {
			Ext.Msg.alert("提示", "菜单数据错误");
		    return false;
		}
        var tab = this.getMainCenter();
        var at = tab.down(target);
        if(!at){
    		at = tab.add({
    			title    : node.text,
    			xtype: target,
    			itemId: node.id,
    			tabTip   : node.text,
    			border   : false,
    			closable : true
    		}); 
    	}
    	tab.remove(tab.down('homepage'),true); 
    	tab.setActiveTab(at);
    }
});